<template>
	<view class="page page-fill">
		<view class="face">
		<image class="regsit-img" src="../../static/img/me/regsit.png" mode=""></image>
		</view>
		
		<form @submit="fromSubmit">
		    <view class=" loginIsregistd-wrap">
			<view class="userName loginIsregist">
				<label class="user-label">账号</label>
				<input type="text" name = "username" value="" class="user-input" placeholder="请输入用户名" />
			</view>
			
			<view class="userPassword loginIsregist">
				<label class="user-label">密码</label>
				<input type="password" name ="password"value="" class="password-input" placeholder="请输入密码" />
			</view>
			</view>
		<button type="primary" form-type="submit" class="loginRegistBtn">注册/登录</button>
		</form>
	
		
		
		
		
	 <!-- #ifndef H5 -->
		<view class="third-login">
			<view class="line-wrap">
				<view class="line"></view>
				<view class="line-text">第三方登录</view>
			</view>
			
			<!-- #ifdef APP-PLUS -->
			   <view class="third-wrap">
			   	  <image src="../../static/icon/weixin-icon.png" data-loginType = "weixin" @click = "appOauthLogin" class="third-icon"mode=""></image>
			   	  <image src="../../static/icon/qq-icon.png" data-loginType="qq"class="third-icon" mode=""></image>
			   	  <image src="../../static/icon/weibo-icon.png" data-loginType="sinawebo"class="third-icon"></image>
			   </view>
			   	<!-- #endif -->
			
		    <!-- #ifdef MP-WEIXIN -->
		         <button class="third-btn" open-type="getUserInfo" @getuserinfo="wxLogin"></button>
		    <!-- #endif -->
			
		</view>
	<!-- #endif -->

		
	</view>
	
	
</template>

<script>
	export default {
		data() {
			return {
				
			}
		},
		methods: {
			fromSubmit(e){
			  var userInfo = e.detail.value,
			     
			      password = userInfo.password,
				  username = userInfo.username;
				  
				  uni.request({
				  	method:'POST',
					url:this.serverUrl+'/user/registOrLogin?qq=3316368400',
					data:{
						"username":username,
						"password":password
					},
					success:(res)=>{
					 let data = res.data;
					 console.log(data);
					  if(data.status ===200){
						 uni.setStorageSync("globalInfo",data.data);
						 uni.switchTab({
						 	url:'../me/me'
						 }) 
					  }else if(data.status === 500){
						  uni.showToast({
						  	title:"登录失败",
							image:"../../static/icon/error.png"
						  })
					  }
					  
					   
					}
				  })
			},
			appOauthLogin(e){
				//获取登录类型
				let loginType = e.detail.currentTarget.logintype;
				 //用户授权调用uni.login();
				 uni.login({
				 	provider:loginType,
					success:(reslutInfo)=>{
						
						//授权登录以后，获取用户信息调用uni.getUserInfo;
						 uni.getUserInfo({
							 provider:loginType,
							 success:(info)=>{
								 var userInfo = info.userInfo,
								     face = "",
									 nickname = "",
									 openIdOrUid = "";
									 if(loginType==="weixin"){
										 face = userInfo.avatarUrl;
										 nickname = userInfo.nickName;
										 openIdOrUid = userInfo.openId;
									 }else if(loginType === "qq"){
										 face = userInfo.figureurl_qq_2;
										 nickname = userInfo.nickname;
										 openIdOrUid = userInfo.openId;
									 }else if(login==="sinaweibo"){
										 face = userInfo.avatar_large;
										 nickname = userInfo.nickname;
										 openIdOrUid = userInfo.Uid;
									 }
									 
									 uni.request({
										 url:urls,
										 data:{
											 "openIdOrUid":openOrUid,
											 "face":face,
											 "nickname":nickname
										 },
										 method:"POST",
										 success:(result)=>{
											 if(result.data.status ===200){
												 var userInfo = result.data.data;
												   uni.setStorageSync('globalInfo');
												   uni.switdhTab({
													   url:'../me/me'
												   })
											 }
										 }
							
									 })
							 }
						 })
					}
				 })
			},
			wxLogin(e) {
							var me = this;
							// console.log(e);
							// 通过微信开发能力，获得微信用户的基本信息
							var userInfo = e.detail.userInfo;
							
							// 实现微信登录
							uni.login({
								provider: "weixin",
								success(loginResult) {
									// console.log(loginResult);
									// 获得微信登录的code：授权码
									var code = loginResult.code;
									// 设置登录到哪个对于的微信小程序，大家可以根据自己的后端去实现业务参数
									// [0:NEXT超英预告][1:超英预告][2:NEXT学院电影预告]
									var loginToWhichMP = 1;
									uni.request({
										url: me.serverUrl + "/mpWXLogin/" + code+"?qq=3316368400",
										data: {
											"avatarUrl": userInfo.avatarUrl,
											"nickName": userInfo.nickName,
											"whichMP": loginToWhichMP
										},
										method: "POST",
										success(userResult) {
											console.log(userResult);
											var userInfo = userResult.data.data;
											// 保存用户信息到全局的缓存中
											uni.setStorageSync("globalInfo", userInfo);
											// 切换页面跳转，使用tab切换的api
											uni.switchTab({
												url: "../me/me"
											});
										}
									});
									
								}
							})
						},
		}
	}
</script>

<style>
  @import url("regist.css");
</style>
